<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //计算选中水果的总价
       let arr = [
        {id:1,name:'香蕉',state:true,price:5,count:10},
        {id:2,name:'苹果',state:false,price:4,count:3},
        {id:3,name:'葡萄',state:true,price:3,count:1}
    ]
    //1.普通实现
    // let price_all = 0;
    // arr.filter(item => item.state).forEach(value=>{
    //     price_all += value.price * value.count
    // })
    // console.log(price_all); //53

    //使用reduce方法实现
    let result = arr.filter(item => item.state).reduce((price_all,value)=>{
        return price_all += value.price * value.count;
    },0)

    console.log(result);
    </script>
    
</body>
</html>